<template>
  <div class="login">
     <div class="login-box">
       <!-- 头像区域 -->
       <div class="avatar-box">
         <img src="../assets/logo.png" alt="">
       </div>
       <!-- 表单区域 -->
       <el-form ref="loginRef" class="login-form" :model="loginForm" :rules="rules">
        <el-form-item prop="username">
          <el-input type="text" prefix-icon="el-icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-tianchongxing-" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item class="login-btn">
          <el-button type="primary" @click="signUp">登录</el-button>
          <el-button type="info" @click="loginRest">重置</el-button>
        </el-form-item>
       </el-form>
     </div>
  </div>
</template>
<script>
export default {
  name: 'login',
  data () {
    var checkName = function (rule, value, callback) {
      console.log(value)
      if (!value) {
        return callback(new Error('用户名不能为空'))
      }
      if (value.length < 3 || value.length > 5) {
        return callback(new Error('用户名长度在3-5个字符之间'))
      } else {
        callback()
      }
    }
    return {
      loginForm: { username: 'admin', password: '123456' },
      rules: {
        username: [
          { validator: checkName, trigger: 'blur' }
        ],
        password: [
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' },
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    loginRest () {
      this.$refs.loginRef.resetFields()
    },
    signUp () {
      // 为什么只判断不能为空，不判断字符长度 因为一进入页面就可以点击登录按钮
      // 而如果你输入了字符，表单会自动验证你的输入内容，验证不通过，你点击登录按钮是没用的
      // if (!this.loginForm.username) {
      //   return this.$message.error('用户名不能为空')
      // }
      // if (!this.loginForm.password) {
      //   return this.$message.error('密码不能为空')
      // }
      // element-ui 有一个更简单的方法自动验证
      this.$refs.loginRef.validate(async (yanzheng) => {
        if (yanzheng) {
          const { data: res } = await this.$http.post('login', this.loginForm)
          console.log(res)
          if (res.meta.status === 200) {
            this.$message.success('登录成功')
            /* 登录成功后服务器会返回一个token 需要保存到sessionStorage 只在会话期间有效 */
            window.sessionStorage.setItem('token', res.data.token)
            /* 通过编程式导航跳转到后台主页home 路由地址是 /home */
            this.$router.push('/home')
          } else {
            this.$message.error('登录失败')
          }
        }
      })
    }
  }
}
</script>
<!-- Add "scoped" attribute 自在当前组件生效 防止样式冲突 -->
<style scoped>
  .login{
    height: 100%;
    background-color:#2b4b6b;
    position: relative;
  }
  .login-box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
  }
  .avatar-box{
    position: absolute;
    left:50%;
    top:-30%;
    margin-left: -75px;
  }
  .avatar-box>img{
    width:130px;
    height:130px;
    border:1px solid green;
    background-color: #fff;
    border-radius: 50%;
    box-shadow: 5px 5px 2px 2px gold;
  }
  .login-form{
    position: absolute;
    bottom:0;
    width:100%;
    box-sizing: border-box;
    padding:10px;
  }
  .login-btn{
    display:flex;
    justify-content: flex-end;
  }
</style>
